//
//  XLModifyPSView.m
//  industry
//
//  Created by xl on 2020/12/10.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLModifyPSView.h"
#import "XLModifyPSViewProtocol.h"
#import "XLModifyPSAdapter.h"
#import "XLModifyPSFooterView.h"
#import "XLModifyPSVC.h"
#import "XLModifyPSPresenterProtocol.h"
#import "NSString+XL.h"

@interface XLModifyPSView ()<XLModifyPSViewProtocol>

@property (nonatomic, strong) AUToast *toastLoad;
@property (nonatomic, strong) XLModifyPSAdapter *adapter;
@property (nonatomic, strong) XLTableView *tableView;
@property (nonatomic, strong) XLModifyPSFooterView *footerView;

@end
@implementation XLModifyPSView

- (void)dealloc {
    XLLog(@"XLModifyPSView dealloc");
}

#pragma mark - XLModifyPSViewProtocol
- (void)buildView {
    [self addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    XLModifyPSVC *vc = (XLModifyPSVC *)self.baseController;
    if (vc.fromType == XLModifyPSVCFromTypeNext) {
        [_footerView.sendBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_adapter setAdapterArray:@[XLModifyPSAdapterOldPS]];
    } else {
        if (vc.companyArr.count > 0) {
            NSString *str = [vc.companyArr componentsJoinedByString:@"、"];
            NSString *title = [NSString stringWithFormat:@"*当前为内部员工登录，修改密码同时会修改 %@ 系统登录密码", str];
            NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:title];
            for (NSString *content in vc.companyArr) {
                [attri setAttributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:[title rangeOfString:content]];
            }
            CGFloat height = [title calculateRowHeightWithFont:[UIFont fontOfPingFangSCRegularWithSize:12] width:(SCREEN_WIDTH - 30*2)];
            _footerView.reasonLB.attributedText = attri;
            _footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 75 + ceilf(height));
        }
        [_footerView.sendBtn setTitle:@"修改密码" forState:UIControlStateNormal];
        [_adapter setAdapterArray:@[XLModifyPSAdapterNewPS, XLModifyPSAdapterNewAgainPS]];
    }
    [_tableView reloadData];
}

- (void)showLoadingAnimalWithTitle:(NSString *)title {
    self.toastLoad = [XLToast showLoadingTitle:title superView:self];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

#pragma mark - UIButton
- (void)clickSend {
    [self endEditing:YES];
    XLModifyPSVC *vc = (XLModifyPSVC *)self.baseController;
    if (vc.fromType == XLModifyPSVCFromTypeNext) {
        if (ISEmptyString(_adapter.oldPs)) {
            [XLToast showComonMessage:@"请填写旧密码"];
            return;
        }
        XLSend(self.baseController.presenter, XLModifyPSPresenterProtocol, requestFetchAESKey:_adapter.oldPs);
    } else {
        if (ISEmptyString(_adapter.ps)) {
            [XLToast showComonMessage:@"请填写新密码"];
            return;
        }
        if (ISEmptyString(_adapter.rePs)) {
            [XLToast showComonMessage:@"请再次填写新密码"];
            return;
        }
        if (![_adapter.ps isEqualToString:_adapter.rePs]) {
            [XLToast showComonMessage:@"前后密码不一致"];
            return;
        }
        XLSend(self.baseController.presenter, XLModifyPSPresenterProtocol, requestFetchAESKey:_adapter.ps);
    }
}

#pragma mark - Setter/getter
- (XLTableView *)tableView {
    if (!_tableView) {
        _tableView = [[XLTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteBgColor];
        _tableView.delegate = self.adapter;
        _tableView.dataSource = self.adapter;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (XLModifyPSAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[XLModifyPSAdapter alloc] init];
    }
    return _adapter;
}

- (XLModifyPSFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[XLModifyPSFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75)];
        [_footerView.sendBtn addTarget:self action:@selector(clickSend) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footerView;
}


@end
